<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true"
          >添加分类</el-button
        >
      </div>
      <el-table :data="artCateList" style="width: 100%" border>
        <el-table-column
          type="index"
          label="序号"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="cate_name"
          label="分类名称"
          width="680"
        ></el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template v-slot="{row}">
          <el-button type="primary" icon="el-icon-edit" @click="showEditDialog(row.id)" circle></el-button>
          <el-button type="danger" icon="el-icon-delete" @click="delArtCate(row.id)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹出层 -->
    <!-- 添加文章分类 -->
    <el-dialog title="添加分类" :visible.sync="addVisible" width="30%">
      <el-form ref="addArtCate" :rules="rulesArtCate" :model="addArtCate" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addArtCate.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addArtCate.cate_alias"></el-input>
        </el-form-item>
        <el-form-item label="分类别名">
          <el-button type="primary" @click="addArtCateHandler" >立即添加</el-button>
            <el-button @click="resetForm('addArtCate')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 修改文章分类 -->
     <el-dialog title="修改分类" :visible.sync="editVisible" width="30%">
      <el-form ref="editArtCate" :rules="rulesArtCate" :model="editArtCate" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editArtCate.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editArtCate.cate_alias"></el-input>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" @click="editArtCateHandler" >立即修改</el-button>
            <el-button @click="resetForm('editArtCate')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  name: 'ArtCate',
  data () {
    return {
      addVisible: false,
      editVisible: false,
      addArtCate: {
        cate_name: '',
        cate_alias: ''
      },
      editArtCate: {},
      rulesArtCate: {
        cate_name: [
          { required: true, message: '请选输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '非空字符长度为1-10', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请选输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '只能是字母数字，长度1-15', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions('article', ['getArtCateList']),
    resetForm (Form) {
      this.$refs[Form].resetFields()
    },
    addArtCateHandler () {
      this.$refs.addArtCate.validate(async valid => {
        if (!valid) return this.$message.error('请正确填写')
        const { data: res } = await this.$http.post('/my/cate/add', this.addArtCate)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addVisible = false
        this.$refs.addArtCate.resetFields()
        this.getArtCateList()
      })
    },
    editArtCateHandler () {
      this.$refs.editArtCate.validate(async valid => {
        if (!valid) return this.$message.error('请正确填写')
        const { data: res } = await this.$http.put('/my/cate/info', this.editArtCate)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.editVisible = false
        this.$refs.editArtCate.resetFields()
        this.getArtCateList()
      })
    },
    async showEditDialog (id) {
      // console.log(id)
      const { data: res } = await this.$http.get('/my/cate/info', { params: { id } })
      // console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.editArtCate = res.data
      this.editVisible = true
    },
    async delArtCate (id) {
      const result = await this.$confirm('确认删除？', '警告!').catch(e => e)
      // console.log(result)
      if (result === 'confirm') {
        const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getArtCateList()
      } else {
        this.$message.success('取消删除成功')
      }
    }
  },
  created () {
    this.getArtCateList()
  },
  computed: {
    ...mapState('article', ['artCateList'])
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
